package TypeWriter;
import java.awt.BorderLayout;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

import Room.Room;
import Actions.Events;
import Actions.Windows;


public class TypeWriter extends JFrame {

	private static final long serialVersionUID = 1L;


	// the starter
	public static void main(String[] arg) {
		new TypeWriter(arg);
	}
	
	private Room editor;
	private JScrollPane scroll;
		
	public static final int INIT_W = 700;
	public static final int INIT_H = 420;
	
	
	private Actions.Files fileSystem;
	private Actions.Windows windowSystem;
	private Actions.Events eventSystem;
	private Options options;
	
	
	// contruct it up
	public TypeWriter(String[] args)
	{
		// construct editor
		editor = new Room();
		
		// set up scrollable area
		scroll = new JScrollPane(editor,JScrollPane.VERTICAL_SCROLLBAR_NEVER,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		add(scroll,BorderLayout.CENTER);
//		scroll.setPreferredSize(new Dimension(INIT_W,INIT_H));
		scroll.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
				
		// filesystem handler
		fileSystem = new Actions.Files(this,editor);
		
		// set states
		editor.setModified(false);
		

		// options - must come before editor.start()
		options = new Options(this);
		// Init the window item
		windowSystem = new Windows(this,scroll,editor);
		// init event system
		eventSystem = new Events(this,windowSystem,editor);
				
		// load options
		options.load();
				
		// start the editor room
		editor.start(this);
		
		// read args, open files if needed
		options.readArgs(args);
		
		// sets teh keybindings... no more add key listener
		eventSystem.setKeyBindings();
		
		// stuff
		pack();
		
		// add listeners to the editor
		editor.addKeyListener(eventSystem.key);
		editor.addMouseWheelListener(eventSystem.mouseWheel);
		this.addWindowListener(eventSystem.wnd);

		// update title
		changeTitle();
		
		// set window icon
		setIconImage( new ImageIcon( TypeWriter.class.getResource("icon.png") ).getImage() );
		
		// light it up
		setVisible(true);
		// start this stuff
		setBounds( options.startX, options.startY , options.startW, options.startH);
		
		windowSystem.smallMargins();
		
		// toggle to fullscreen
		if(options.startFullScreen)
			windowSystem.toggleFullScreen();
		
	}
	
	public JScrollPane getScroller()
	{
		return scroll;
	}
	
	
	// get the ile system
	public Actions.Files getFiles()
	{
		return fileSystem;
	}
	public Options getOptions()
	{
		return options;
	}
	public Room getEditor()
	{
		return editor;
	}
	public Events getEvents()
	{
		return eventSystem;
	}
	public Windows getWindowSys()
	{
		return windowSystem;
	}
	
	public void changeTitle()
	{
		setTitle(editor.getFileName() + ( (editor.getModified()) ? "* "  : " " ) + "- TypeWriter ");
	}
	
	
}
